#include "emptybottletest.h"
#include "QDebug"
#include <QTimer>
#include <QFile>
#include "application.h"
#include "event.h"
#include "eventid.h"
#include "curve.h"

EmptyBottleTest::EmptyBottleTest(QObject *parent)
    : QObject(parent)
{
    m_emptyBottleDetector = new EmptyBottleDetector();
    m_emptyBottleDetector->setPressureThreshold(-5, -1); //
    m_emptyBottleDetector->setEnable(true);
    m_emptyBottleDetector->setReStart(true);
    m_emptyBottleDetector->setMaskTime(10 * 60); // 前10min不处理
//    m_emptyBottleDetector->print();
}

EmptyBottleTest::~EmptyBottleTest()
{
    delete m_emptyBottleDetector;
}

void EmptyBottleTest::start()
{
    qDebug() << "1===============================================";
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac12_200ml_h.txt", "r");
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac14_200ml_h.txt", "r");
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac14_25ml_h.txt", "r");
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/angui16bitadc_200ml_h.txt", "r");
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac14_500ml_h.txt", "r");
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac14_25ml_h_2.txt", "r");
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac14_1ml_h.txt", "r");

//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac15_200ml_h.txt", "r");
//    FILE *fp = fopen("/home/jiehua/pressure_data/inletpressure/ande_emptybottle/mac15_25ml_h__1.txt", "r");

//    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac15/2022-12-14_09:41:19.txt", "r");
//    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac15/2022-12-14_11:14:29.txt", "r");
//    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac17/2022-12-14_11:13:13.txt", "r");
//    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac17/2022-12-14_13:47:10.txt", "r");
//    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac15/2022-12-14_13:46:31.txt", "r");

//    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac15/2023-02-22_14:07:12.txt", "r");
//    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac15/2023-02-22_15:00:00.txt", "r");
    FILE *fp = fopen("/home/jiehua/svn/infusion/output/bin/work/debugdata/inlet_pressure/mac15/2023-02-24_11:12:38.txt", "r");
    if (fp == nullptr)
        return;

    qDebug() << "2===============================================";
    QTimer::singleShot(1000, this, [=](){
        qDebug() << "3===============================================";
        static double s_data3 = qQNaN();
        while(1)
        {
            char buffer[100];
            fgets(buffer, 100, fp);
            QString str(buffer);
            if (!str.isEmpty() && !feof(fp))
            {
                QStringList list = str.split(' ');
    //            bool ok = false;
    //            list.at(2).toInt(&ok);
    //            if (!ok)
    //                continue;
                double data1 = list.at(2).toDouble();
                double data3 = list.at(3).toDouble();

    //            double data1 = list.at(1).toDouble();
    //            double data3 = list.at(3).toDouble();

                if (qIsNaN(s_data3)) // 第一个数据
                {
                    s_data3 = data3;
                }

                // 检查泵轴是否转了一圈
                bool isShaftPeriod = (s_data3 != data3);
                s_data3 = data3;

                m_emptyBottleDetector->addData(data1);

                {
                    QPointF point;
                    QVariant var;
                    PlotXYParameter parameter;

                    point = m_emptyBottleDetector->getCurrent();
                    parameter.num = 0;
                    parameter.x = point.x();
                    parameter.y = point.y();
                    var.setValue(parameter);
                    plotXY(var);
                }

                if (isShaftPeriod)
                {
                    if (m_emptyBottleDetector->detect())
                    {
    //                    QList<QPointF> maxBuf = m_emptyBottleDetector->getMaxBuffer();
    //                    qDebug() << "____________________emptyBottleDetector_______________________"
    //                             << maxBuf.last();

    //                    m_emptyBottleDetector->clear();
                    }

                    QPointF point;
                    QVariant var;
                    PlotXYParameter parameter;

                    point = m_emptyBottleDetector->getMax();
                    if (!qIsNaN(point.x()))
                    {
                        parameter.num = 1;
                        parameter.x = point.x();
                        parameter.y = point.y();
                        var.setValue(parameter);
                        plotXY(var);
                    }

                    point = m_emptyBottleDetector->getDelta();
                    if (!qIsNaN(point.x()))
                    {
                        parameter.num = 2;
                        parameter.x = point.x();
                        parameter.y = point.y();
                        var.setValue(parameter);
                        plotXY(var);
                    }

                    point = m_emptyBottleDetector->getDelta2();
                    if (!qIsNaN(point.x()))
                    {
                        parameter.num = 3;
                        parameter.x = point.x();
                        parameter.y = point.y();
                        var.setValue(parameter);
                        plotXY(var);
                    }
                }
            }
            else
            {
                qDebug() << "The file is empty";
                fclose(fp);

    #if 0
    //            QList<QPointF> buf = m_emptyBottleDetector->getMaxBuffer();
                QList<QPointF> buf = m_emptyBottleDetector->getDeltaBuffer();
    //            QList<QPointF> buf = m_emptyBottleDetector->getDelta2Buffer();
                for (int i = 0; i < buf.size() - 1; i++)
                {
                    QVariant var;
                    PlotXYParameter parameter;
                    parameter.num = 0;
                    parameter.x = buf.at(i).x();
                    parameter.y = buf.at(i).y();
                    var.setValue(parameter);
                    plotXY(var);
                }
    #endif

                break;
            }
        } // while(1)
    }); // QTimer::singleShot
}

void EmptyBottleTest::plotXY(QVariant var)
{
    CustomEvent event((QEvent::Type)EV_CurveTest);
    event.var.setValue(var);
    Application::getInstance()->publish(&event);
}
